package com.liuyi.animtest;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ArgbEvaluator;
import android.animation.FloatEvaluator;
import android.animation.IntEvaluator;
import android.animation.Keyframe;
import android.animation.LayoutTransition;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.TimeInterpolator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.BounceInterpolator;
import android.widget.TextView;

/**
 * 
 * @author Crystal
 * 
 * AccelerateDecelerateInterpolator 在动画开始与介绍的地方速率改变比较慢，在中间的时候加速
AccelerateInterpolator 在动画开始的地方速率改变比较慢，然后开始加速
AnticipateInterpolator 开始的时候向后然后向前甩
AnticipateOvershootInterpolator 开始的时候向后然后向前甩一定值后返回最后的值
BounceInterpolator 动画结束的时候弹起
CycleInterpolator 动画循环播放特定的次数，速率改变沿着正弦曲线
DecelerateInterpolator 在动画开始的地方快然后慢
LinearInterpolator 以常量速率改变
OvershootInterpolator 向前甩一定值后再回到原来位置
 *
 */

public class MainActivity extends Activity {
	
	private TextView mTextView;
	private TextView mTextView2;
	private Animation mScaleAnimation;
	private Animation mAlpahaAnimation;
	private Animation mRotateaAnimation;
	private Animation mTranslateAnimation;
	private Animation mAnimationSet;
	
	private AnimationSet mAnimationSet2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mAlpahaAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha);
		mScaleAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scale);
		mRotateaAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.ratate);
		//平移动画，就算用的是相对布局，其相对控件也不会随着动画的改变而改变
		mTranslateAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.translate);
		
		mAnimationSet = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.set);
		mAnimationSet2 = new AnimationSet(true);
		mAnimationSet2.addAnimation(mAlpahaAnimation);
		mAnimationSet2.addAnimation(mScaleAnimation);
		mAnimationSet2.addAnimation(mRotateaAnimation);
		mAnimationSet2.addAnimation(mTranslateAnimation);
		
		mTextView = (TextView) findViewById(R.id.tv);
		mTextView2 = (TextView) findViewById(R.id.tv2);
		//只有在设置的当前时间有效，后面通过点击事件再去设置不会出现动画效果
//		mTextView.setAnimation(mScaleAnimation);
		mTextView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				mTextView.startAnimation(mAnimationSet);
			}
		});
		
		
		//=============ValueAnimator==============================
		
		ValueAnimator valueAnimator = ValueAnimator.ofInt(0,640,120);
		valueAnimator.setEvaluator(new IntEvaluator());
		valueAnimator.setDuration(2000);
		
//		Interpolator系列
//		BounceInterpolator:弹跳插值器
//		LinearInterpolator:匀速返回区间点的值
//		DecelerateInterpolator:则表示开始变化快，后期变化慢
//		AccelerateInterpolator等等
//		自定义插值器
		
//		Evaluator系列，只有在数值类型相同的情况下，Evalutor 才能共用
//		IntEvaluator对应ofInt
//		FloatEvaluator对应ofFloat
//		ArgbEvalutor对应ofInt或者ofArgb都可以
//		自定义Evaluator
//		valueAnimator.setEvaluator(new MyEvaluator());
		valueAnimator.setInterpolator(new MyTimeInterpolator());
//		valueAnimator.setRepeatMode(ValueAnimator.RESTART);
//		valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
		valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
			
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				int curr = (Integer)animation.getAnimatedValue();
				mTextView.layout(curr, curr, curr + mTextView.getWidth(), curr + mTextView.getHeight());
			}
		});
		valueAnimator.addListener(new AnimatorListener() {
			
			@Override
			public void onAnimationStart(Animator animation) {
				
			}
			
			@Override
			public void onAnimationRepeat(Animator animation) {
				
			}
			
			@Override
			public void onAnimationEnd(Animator animation) {
				
			}
			
			@Override
			public void onAnimationCancel(Animator animation) {
				
			}
		});
		valueAnimator.start();
		valueAnimator.cancel();
//		valueAnimator.start();
		
		//====================================================================================
		ValueAnimator valueAnimator2 = ValueAnimator.ofInt(0xffffff00,0xff0000ff);
//		支持自定义，如下
//		ValueAnimator valueAnimator3 = ValueAnimator.ofObject(evaluator, values);
		valueAnimator2.setEvaluator(new ArgbEvaluator());
		valueAnimator2.setDuration(2000);
		valueAnimator2.addUpdateListener(new AnimatorUpdateListener() {
			
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				int curr = (Integer)animation.getAnimatedValue();
				mTextView2.setBackgroundColor(curr);
			}
		});
		valueAnimator2.start();
		
		//============================ObjectAnimator========================================================
//		第二个参数对应的就是第一个参数的属性，一定要是setXXX里面有的XXX
		ObjectAnimator animator = ObjectAnimator.ofFloat(mTextView,"alpha",1,0,1);  
		animator.setDuration(2000);  
		animator.start(); 
		
		ObjectAnimator animator2 = ObjectAnimator.ofFloat(mTextView,"rotation",0,180,0);  
		animator2.setDuration(2000);  
		animator2.start();
		
		ObjectAnimator animator3 = ObjectAnimator.ofFloat(mTextView,"rotationX",0,270,0);  
		animator3.setDuration(2000);  
		animator3.start();
		
		ObjectAnimator animator5 = ObjectAnimator.ofFloat(mTextView, "translationX", 0, 200, -200,0);  
		animator5.setDuration(2000);  
		animator5.start();
		
		ObjectAnimator animator4 = ObjectAnimator.ofFloat(mTextView, "scaleX", 0, 3, 1);  
		animator4.setDuration(2000);  
		animator4.start();
		
		//可以设置多个
		PropertyValuesHolder rotationHolder = PropertyValuesHolder.ofFloat("rotation", 60f, -60f, 40f, -40f, -20f, 20f, 10f, -10f, 0f);  
		PropertyValuesHolder colorHolder = PropertyValuesHolder.ofInt("backgroundColor", 0xffffffff, 0xffff00ff, 0xffffff00, 0xffffffff);  
		ObjectAnimator animator6 = ObjectAnimator.ofPropertyValuesHolder(mTextView, rotationHolder, colorHolder);  
	
		//Keyframe:关键帧，相当于flash制作时候的关键帧
//		第一个参数表示进度的百分值
//		第二个参数为具体的值
//		至少需要两个关键帧，否则会报错
		Keyframe frame0 = Keyframe.ofFloat(0f, 0);  
		Keyframe frame1 = Keyframe.ofFloat(0.1f, -20f);  
		Keyframe frame2 = Keyframe.ofFloat(1.0f, 0);  
		frame1.setInterpolator(new BounceInterpolator());
		PropertyValuesHolder frameHolder = PropertyValuesHolder.ofKeyframe("rotation",frame0,frame1,frame2);  
		Animator animator7 = ObjectAnimator.ofPropertyValuesHolder(mTextView,frameHolder);  
		animator7.setDuration(2000);  
		animator7.start();
	
//		========================================================================
//		playSequentially 表示所有动画依次播放
//		playTogether 表示所有动画一起开始
//		AnimatorSet animatorSet = new AnimatorSet(); 
//		animatorSet.playTogether(tv1BgAnimator,tv1TranslateY,tv2TranslateY); 
//		animatorSet.setDuration(2000); 
//		animatorSet.start();
		
		//===================自由设置动画顺序——AnimatorSet.Builder==============================
		//和前面动画一起执行 public Builder with(Animator anim) 
		//执行前面动画前执行动画 public Builder before(Animator anim) 
		//执行前面的动画后执行该动画 public Builder after(Animator anim) 
		//延迟 n 毫秒之后执行动画 public Builder after(long delay)
//		animatorSet.play(tv1TranslateY).with(tv2TranslateY).after(tv1BgAnimator);
		
//		在 AnimatorSet 中设置以后，会覆盖单个 ObjectAnimator 中的设置；
//		即如果 AnimatorSet 中没有设置，那么就以 ObjectAnimator 中的设置为准。
//		如果 AnimatorSet 中设置以后，ObjectAnimator 中的设置就会无效
		
		
		Animator animator8 = (ObjectAnimator) AnimatorInflater.loadAnimator(this,R.animator.animator);
		animator.setTarget(mTextView);
		animator.start();
	}
	
	
	@Override  
    public void onWindowFocusChanged(boolean hasFocus) {  
        super.onWindowFocusChanged(hasFocus);  
        
        mTextView.setBackgroundResource(R.drawable.list_anim);  
        AnimationDrawable anim = (AnimationDrawable) mTextView.getBackground();  
        anim.start();  
    }
	
	/**
	 * 通过这个来确定返回的百分数进度
	 * @author Crystal
	 *
	 */
	private class MyTimeInterpolator implements TimeInterpolator{

		/**
		 * input:表示时间进度,范围0-1(setDuration(XXX))
		 * 返回值:表示空间进度,范围0-1(.ofInt(0,640))
		 * 
		 * onAnimationUpdate里面(Integer)animation.getAnimatedValue();
		 * 获取到的返回值为getInterpolation返回的一个百分数值*(640-0)+0
		 */
		@Override
		public float getInterpolation(float input) {
			return 1.0f-input;
		}
	}

	/**
	 * 通过这个来确定返回的具体位置进度
	 * @author Crystal
	 */
	private class MyEvaluator implements TypeEvaluator<Integer> {

		@Override
		public Integer evaluate(float fraction, Integer startValue,
				Integer endValue) {
			
			return (int) (200 + startValue + fraction*(endValue - startValue));
		}
	}
}
